package io.esirong.base;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.esirong.base.mvp.IBasePresenter;
import io.esirong.base.mvp.IBaseView;

/**
 * @author 黄嵘才(HuangRc)
 */
public abstract class BaseMvpFragment<P extends IBasePresenter> extends BaseFragment implements IBaseView {
    protected P mPresenter;

    protected P presenter() {
        return mPresenter;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //创建present
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachView(this);
        }
    }

    @Override
    protected void initView(View view) {

    }

    /**
     * 创建Presenter
     */
    protected abstract P createPresenter();

    @Override
    public ContextX getContextX() {
        return new ContentXWrapper(getContext());
    }
}
